<?php
/**
 * Created by PhpStorm.
 * User: yyq
 * Date: 2017/12/22
 * Time: 17:13
 */

namespace Information\Controller;


use Common\Controller\AdminbaseController;
use Common\Model\AdModel;

class AdsController extends AdminbaseController
{
    private $ad_model;

    public function __construct()
    {
        parent::__construct();
        $this->ad_model = new AdModel();
    }

    public function lists() {
        $result = $this->ad_model->select();

        foreach ($result as $k => $v) {
            $result[$k]['str_manage'] = '<a class="" href="' . U('Ads/edit', ['id' => $v['ad_id']]) . '">编辑</a>';
//            $result[$k]['str_manage'] .= " | ";
//            $result[$k]['str_manage'] .= '<a class="js-ajax-delete" href="' . U('About/delete', ['id' => $v['id']]) . '">删除</a>';

            $categorys .= '<tr>
            <td>' . ($k + 1) . '</td>
            <td>' . $result[$k]['ad_name'] . '</td>
            <td>' . $result[$k]['str_manage'] . '</td>
        </tr>';
        }

        $this->assign('categorys', $categorys);
        $this->display();
    }


    public function edit() {
        if(IS_POST) {
            $data = I('post.');
            $data['ad_content'] = htmlspecialchars_decode($data['ad_content']);//dump($data);exit;
            if(!$this->ad_model->create($data)) {
                $this->error($this->document_model->getError());

            }
            if($this->ad_model->save()) {
                $this->success();
            } else {
                $this->error();
            }
        } else {
            $ad_id = I('get.id');
            $info = $this->ad_model->find($ad_id);
            $this->assign('info', $info);//dump($info);
            $this->display();
        }
    }

    public function edit_post() {
        $data = I('post.');
        $data['ad_content'] = htmlspecialchars_decode($data['ad_content']);//dump($data);exit;
        if(!$this->ad_model->create($data)) {
            $this->error($this->ad_model->getError());

        }
        if($this->ad_model->save()) {
            $this->success();
        } else {
            $this->error();
        }
    }
}